package com.zsr.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.entity.EduPath;
import com.zsr.example.mapper.EduPathMapper;
import com.zsr.example.service.BusinessService;
import com.zsr.example.service.EduPathService;
import com.zsr.example.vo.PathReq;
import com.zsr.example.vo.front.PathFrontReq;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学习路径 服务实现类
 * </p>
 *
 * @author zsr
 * @since 2022-02-10
 */
@Service
public class EduPathServiceImpl extends ServiceImpl<EduPathMapper, EduPath> implements EduPathService {

    @Override
    public void pageQuery(Page<EduPath> page, PathReq query) {
        QueryWrapper<EduPath> wrapper = new QueryWrapper<>();
        String name = query.getName();
        String categoryId = query.getCategoryId();
        String begin = query.getBegin();
        String end = query.getEnd();

        if (!StringUtils.isEmpty(name)) wrapper.like("name", name);
        if (!StringUtils.isEmpty(categoryId)) wrapper.eq("category_id", categoryId);
        if (!StringUtils.isEmpty(begin)) wrapper.ge("gmt_create", begin);//大于
        if (!StringUtils.isEmpty(end)) wrapper.le("gmt_modified", end);//小于


        baseMapper.selectPage(page, wrapper);
    }

    @Cacheable(value = "path", key = "'selectIndexList'", cacheManager = "redisCacheManager")
    @Override
    public List<EduPath> getIndexList() {
        QueryWrapper<EduPath> wrapper = new QueryWrapper<>();
        wrapper.eq("is_top", 1);
        wrapper.eq("status", 1);
        wrapper.orderByDesc("gmt_create");
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> getFrontPathList(Page<EduPath> pageParam, PathFrontReq pathFrontReq) {
        QueryWrapper<EduPath> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pathFrontReq.getCategoryId())) {
            queryWrapper.eq("category_Id", pathFrontReq.getCategoryId());
        }

        if (!StringUtils.isEmpty(pathFrontReq.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(pathFrontReq.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, queryWrapper);

        List<EduPath> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public void updateBuyCount(String targetId) {
        EduPath path = baseMapper.selectById(targetId);
        path.setBuyCount(path.getBuyCount() + 1);
        baseMapper.updateById(path);
    }
}
